import java.util.PriorityQueue;
class Solotion
{
    public int halveArray(int[] nums)
    {
        PriorityQueue<Double> heap = new PriorityQueue<>((a, b) -> b.compareTo(a));
        //使用优先队列（最大堆）来存储数组中的元素
        double sum = 0.0;
        for (int x : nums) {
            heap.offer((double) x);
            sum += x;
        }
        //  将数组中的元素添加到堆中
        sum /= 2.0;
        int count = 0;
        //将总和减半
        while (sum > 0)
        {
            double t = heap.poll() / 2.0;
            sum -= t;
            count++;
            heap.offer(t);
        }
        /* 不断去出堆中最大元素，减半后重新添加到堆中，直到总和减半 */
        return count;
    }

    public static void main(String[] args)
    {
        Solotion solution = new Solotion();
        int[] nums = {5, 9, 18, 1};
        int result = solution.halveArray(nums);
        System.out.println("Number of opperations:" + result);
    }
}
